package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.BaseInfor.Seminar;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.ListWorkshopsAdapter;
import keeto.com.vn.bms.ui.widget.LogoutDialog;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class ListSeminarActivity extends FragmentActivity implements
OnItemClickListener, OnClickListener, OnRefreshListener<ListView> {

	private static final int pageLimit = 10;
	private  int pageNumber = 1;
	private ArrayList<Seminar> mArrayList = new ArrayList<Seminar>();
	private ListWorkshopsAdapter mAdapter;
	private PullToRefreshListView lvSeminar;
	private ListView mListView;
	private ProgressBar pgbar;
	private ImageButton btnBack;
	private TextView tvTitleName;
	private ImageButton btnLogon;
	private BMSSharedPreferences preferences;
	private ListSeminarListenner listenner= new ListSeminarListenner();

	public static void actionLaunch(Context context) {
		Intent intent = new Intent(context, ListSeminarActivity.class);
		context.startActivity(intent);

	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.fragment_list_infor);
		preferences = new BMSSharedPreferences(this);
		initViewComponent();

	}

	private void initViewComponent() {
		btnBack = (ImageButton) findViewById(R.id.btn_tittle_back);
		tvTitleName = (TextView) findViewById(R.id.tv_title_name);
		btnLogon = (ImageButton) findViewById(R.id.btn_tittle_login);
		lvSeminar = (PullToRefreshListView) findViewById(R.id.lst_product);
		tvTitleName.setText(getString(R.string.txt_list_workshops));
		pgbar=(ProgressBar)findViewById(R.id.pgbar);
		mListView = lvSeminar.getRefreshableView();
		btnLogon.setBackgroundResource(Utils.getLogobyPermission(preferences
				.getUserPermission(this)));

		btnBack.setOnClickListener(this);
		btnLogon.setOnClickListener(this);
		lvSeminar.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		mAdapter = new ListWorkshopsAdapter(ListSeminarActivity.this, mArrayList);
		lvSeminar.setAdapter(mAdapter);
		BMSCore.getInstance(this).taskGetListSeminar(pageLimit, pageNumber, listenner);
		pgbar.setVisibility(View.VISIBLE);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		DetailProductInfoActivity.actionLaunchViewSeminar(ListSeminarActivity.this, mArrayList.get(position-1));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;
		case R.id.btn_tittle_login:
			if (preferences.getUserAuthKey(ListSeminarActivity.this).equals("")){
				LoginBMSActivity.actionLaunch(ListSeminarActivity.this);
			}else{
				LogoutDialog dialog = new LogoutDialog(ListSeminarActivity.this);
				dialog.show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		if(pageNumber>0){
			BMSCore.getInstance(this).taskGetListSeminar(pageLimit, pageNumber, listenner);

		}else{

			lvSeminar.onRefreshComplete();
		}
	}
	class ListSeminarListenner extends BMSListener{

		@Override
		public void finishGetListSeminar(final int returnCode, final int pageCount,
				final ArrayList<Seminar> arrayList) {
			// TODO Auto-generated method stub
			super.finishGetListSeminar(returnCode, pageCount, arrayList);
			runOnUiThread(new Runnable() {
				public void run() {
					pgbar.setVisibility(View.GONE);
					if(returnCode==IBMSProtocol.STATUS_CODE_OK){
						if (pageNumber < pageCount){
							pageNumber +=1;
						}else{
							pageNumber=-1;
						}

						for(Seminar infor: arrayList){
							mArrayList.add(infor);
						}
						mAdapter.notifyDataSetChanged();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ListSeminarActivity.this, ListSeminarActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ListSeminarActivity.this, ListSeminarActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ListSeminarActivity.this, ListSeminarActivity.this.getResources().getString(R.string.error_perpose),Toast.LENGTH_SHORT).show();
					}
					lvSeminar.onRefreshComplete();

				}
			});
		}
	}

}
